#include "xege.h"
#include "event.hpp"
#include <cstdio>
#include <thread>
#include <cmath>
#include <ctime>
#include <fmt/format.h>

long double operator"" pi(long double x) {
	return x*3.14159265357;
}
long double operator"" pi(unsigned long long x) {
	return static_cast<long double>(x)*3.14159265357;
}

constexpr inline POINTD direct2Length (double offsetx, double offsety, double rad, double length) {
	return { offsetx + cos (rad)*length, offsety + sin (rad)*length };
}

int main (int argc, char **argv) {


	WINDOW win (1000, 1000);
	int x = 0, y = 0;
	int offsetx = 500, offsety = 500;
	int count = 0;
	std::time_t tt;
    std::tm* now;
	xege::FONT myfont;
	SDL_Surface *tmp, *tmp2;
	for (; win; win.update ().clear (), delay_fps (60)) {
		// SDL_Log ("sdfasd");
		tt = time ((time_t*)NULL);
		now = std::gmtime(&tt);
		now->tm_hour += 8;
		win.setTitle (fmt::format ("the times: {} {} {}\n", now->tm_hour, now->tm_min, now->tm_sec));
		offsetx -= 22;
		offsety -= 22;
		for (int i = 0; i < 60; i ++) {
			if ((i)%5==0) {
				tmp = myfont.renderFont (fmt::format ("{:02d}h",(now->tm_hour+i/5)%24));
				if (tmp) {
					RECT dstPicRect {
						offsetx + sin(((14-i)%60+1)/60.0*2pi)*370-4,
						offsety - cos(((14-i)%60+1)/60.0*2pi)*370,
						0, 0
					};
					tmp2 = rotozoomSurface(tmp,i*6,1.0,10);
					SDL_BlitSurface (tmp2, nullptr, win.getSurface (), &dstPicRect);
					// delete tmp;
					win.updateTexture ();
					SDL_FreeSurface (tmp2);
					SDL_FreeSurface (tmp);
				}
			}
			tmp = myfont.renderFont (fmt::format ("{:02d}m",(now->tm_min+i)%60));
			if (tmp) {
				RECT dstPicRect {
					offsetx + sin(((14-i)%60+1)/60.0*2pi)*310-4,
					offsety - cos(((14-i)%60+1)/60.0*2pi)*310,
					0, 0
				};
				tmp2 = rotozoomSurface(tmp,i*6,1.0,10);
				SDL_BlitSurface (tmp2, nullptr, win.getSurface (), &dstPicRect);
				// delete tmp;
				win.updateTexture ();
				SDL_FreeSurface (tmp2);
				SDL_FreeSurface (tmp);
			}
			tmp = myfont.renderFont (fmt::format ("{:02d}s",(now->tm_sec+i)%60));
			if (tmp) {
				RECT dstPicRect {
					offsetx + sin(((14-i)%60+1)/60.0*2pi)*250,
					offsety - cos(((14-i)%60+1)/60.0*2pi)*250,
					0, 0
				};
				tmp2 = rotozoomSurface(tmp,i*6,1.0,10);
				SDL_BlitSurface (tmp2, nullptr, win.getSurface (), &dstPicRect);
				// delete tmp;
				win.updateTexture ();
				SDL_FreeSurface (tmp2);
				SDL_FreeSurface (tmp);
			}
			// win.drawFillCircle (offsetx + sin(((14-i)%60+1)/60.0*2*PI)*220,offsety - cos(((14-i)%60+1)/60.0*2*PI)*220,30);
		}
		
		offsetx += 22;
		offsety += 22;
		win.drawLineW (offsetx, offsety,
				offsetx + sin(now->tm_hour/12.0*2pi)*90,
				offsety - cos(now->tm_hour/12.0*2pi)*90,
			8, XEGERGB (0, 255, 255))
			.drawLineW (offsetx, offsety,
				offsetx + sin(now->tm_min/60.0*2pi)*150,
				offsety - cos(now->tm_min/60.0*2pi)*150,
			4, XEGERGB (0, 255, 0))
			.drawLineW (offsetx, offsety,
				offsetx + sin(now->tm_sec/60.0*2pi)*200,
				offsety - cos(now->tm_sec/60.0*2pi)*200,
			2, XEGERGB (255, 0, 0))
			.drawCircle (offsetx, offsety, 220);
	}
	return 0;
} 

